//
//  BMUtils.m
//  Rail
//
//  Created by hychen on 2018/3/14.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "BMUtils.h"
#import <AFNetworking/AFNetworking.h>

@implementation BMUtils

+ (BOOL)checkOpen4gAutoUploadWithCompletion:(void(^)())completion {
    if ([AFNetworkReachabilityManager sharedManager].isReachableViaWiFi) {
        return YES;
    }
    
    BOOL isOpen = [UserDefaults_bb boolForKey:Key_4G_AutoUpload];
    if (isOpen) {
        return isOpen;
    }
    
    [UIAlertView bk_showAlertViewWithTitle:@"当前为非WiFi环境，是否使用流量上传图片" message:@"" cancelButtonTitle:@"取消" otherButtonTitles:@[@"允许一次",@"总是允许"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
        if ((buttonIndex > 0) && completion) {
            completion();
            if (buttonIndex == 2) {
                [UserDefaults_bb setBool:YES forKey:Key_4G_AutoUpload];
            }
        }
    }];
    
    return isOpen;
}

+ (BOOL)checkOpen4gPlayVideoWithCompletion:(void(^)())completion {
    if ([AFNetworkReachabilityManager sharedManager].isReachableViaWiFi) {
        return YES;
    }
    
    BOOL isOpen = [UserDefaults_bb boolForKey:Key_4G_PlayVideo];
    if (isOpen) {
        return isOpen;
    }
    
    [UIAlertView bk_showAlertViewWithTitle:@"当前为非WiFi环境，是否使用流量播放视频" message:@"" cancelButtonTitle:@"取消播放" otherButtonTitles:@[@"继续播放",@"总是允许"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
        if ((buttonIndex > 0) && completion) {
            completion();
            if (buttonIndex == 2) {
                [UserDefaults_bb setBool:YES forKey:Key_4G_PlayVideo];
            }
        }
    }];
    
    return isOpen;
}

+ (BOOL)checkOpen4gDownloadVideoWithCompletion:(void(^)())completion {
    if ([AFNetworkReachabilityManager sharedManager].isReachableViaWiFi) {
        return YES;
    }
    
    BOOL isOpen = [UserDefaults_bb boolForKey:Key_4G_DownloadVideo];
    if (isOpen) {
        return isOpen;
    }
    
    [UIAlertView bk_showAlertViewWithTitle:@"当前为非WiFi环境，是否使用流量下载视频" message:@"" cancelButtonTitle:@"取消" otherButtonTitles:@[@"允许下载",@"总是允许"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
        if ((buttonIndex > 0) && completion) {
            completion();
            if (buttonIndex == 2) {
                [UserDefaults_bb setBool:YES forKey:Key_4G_DownloadVideo];
            }
        }
    }];
    
    return isOpen;
}

+ (UIColor *)colorWithString:(NSString *)colorString {
    if ([colorString containsString:@"#"]) {
        colorString = [colorString stringByReplacingOccurrencesOfString:@"#" withString:@""];
    }
    if ([colorString containsString:@"0x"]) {
        colorString = [colorString stringByReplacingOccurrencesOfString:@"0x" withString:@""];
    }
    
    NSRange range;
    range.location = 0;
    range.length = 2;
    NSString *rString = [colorString substringWithRange:range];
    range.location = 2;
    NSString *gString = [colorString substringWithRange:range];
    range.location = 4;
    NSString *bString = [colorString substringWithRange:range];
    // Scan values
    unsigned int r, g, b;
    
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];

    UIColor *color = [UIColor colorWithRed:((float) r / 255.0f)
                                     green:((float) g / 255.0f)
                                      blue:((float) b / 255.0f)
                                     alpha:1.0f];
    if (![color isKindOfClass:[UIColor class]]) {
        color = Color_Line;
    }
    return color;
}

+ (NSString *)chineseYYYYMMDDHHmmWithServiceTime:(NSString *)time {
    if ([time isKindOfClass:[NSString class]] && time.length>=19) {
        return [[[time substringToIndex:19] YJ_dateWithYYYYMMDDHHmmsss] formatYearMonthDayHourMinuteChinese_lib]?:@"";
    }
    return @"";
}

+ (NSString *)chineseMMDDHHmmWithServiceTime:(NSString *)time {
    if ([time isKindOfClass:[NSString class]] && time.length>=19) {
        return [[[time substringToIndex:19] YJ_dateWithYYYYMMDDHHmmsss] formatMonthDayHourMinuteChinese_lib]?:@"";
    }
    return @"";
}

+ (NSString *)formatTimeWithServiceTime:(NSString *)time {
    if ([time isKindOfClass:[NSString class]] && time.length>=19) {
        NSDate *date = [[time substringToIndex:19] YJ_dateWithYYYYMMDDHHmmsss];
        if ([date isToday]) {
            NSInteger distance = [date distanceInMinutesToDate:[NSDate date]];
            if (ABS(distance) <= 2) {
                //2分钟内
                return @"刚刚";
            }
            // 10:33
            return [date formatMinuteSecond_lib];
        }
        else if ([date isThisYear]) {
            return [date formatMonthDay_lib];
        }
        else {
            return [date formatYearMonthDayHourMinute_lib];
        }
    }
    return @"";
}
@end
